package com.le.tester.book.concurrentchallenge.shareWithObject;

import java.util.concurrent.TimeUnit;

/**
 * createTime：2021/11/22 10:08
 * description：共享变量的情况下没有可见性
 */
public class NoVisibility {
    private static boolean ready;

    //这个值在并没有设置成可见性，多个线程执行的情况下
    private static int number;

    private static class ReadThread extends Thread {
        @Override
        public void run() {
            //通过自定义静态变量来控制线程执行
            while (!ready)
                Thread.yield();
            System.out.println(number);
        }
    }

    public static void main(String[] args) throws InterruptedException {
        //1.main中本身带了一个线程
        //2.自己new ReadyThread
        //3.ready来控制number的数值
        new ReadThread().start();
        //TimeUnit.SECONDS.sleep(1);
        number = 42;
        ready = true;

    }
}
